package com.xianbeiying.app.ckgl.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.xianbeiying.app.ckgl.bean.CropsBean;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * 字符处理类
 */
public class StringUtil {
    /**
     * 加载本地图片
     *
     * @param url
     * @return
     */
    public static Bitmap getLoacalBitmap(String url) {
        try {
            FileInputStream fis = new FileInputStream(url);
            return BitmapFactory.decodeStream(fis);  ///把流转化为Bitmap图片
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    //编码转换
    public static String utfStr(String utfStr) {
        try {
            return URLDecoder.decode(utfStr, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return utfStr;
    }

    //给数字乘上100
    public static String moneys(String money) {
        double number = Double.valueOf(money);
        return new java.text.DecimalFormat("#").format(number * 100);
    }

    public static String number(String num) {
        double number = Double.valueOf(num);
        return new java.text.DecimalFormat("#").format(number);
    }

    //给数字除上100
    public static String money(String money) {
        double number = Double.valueOf(money);
        return new java.text.DecimalFormat("#0.00").format(number / 100);
    }

//    //给数字除上100
//    public static String doubTwo(String money) {
//        double number = Double.valueOf(money);
//        return new java.text.DecimalFormat("#0.00").format(number);
//    }

    //去掉最后一个,
    public static String getString(StringBuffer stringBuffer) {
        int length = stringBuffer.length();
        return stringBuffer.delete((length - 1), length).toString();
    }

    //设置默认选中框的数量
    public static ArrayList<Boolean> getList(int size){
        ArrayList<Boolean> arrayList = new ArrayList<>();
        int length =size;
        for (int i = 0; i < length; i++){
            arrayList.add(i, false);
        }
        return arrayList;
    }
    //设置默认选中框的数量 (第一个设置为true)
    public static ArrayList<Boolean> getArryList(String[] str) {
        ArrayList<Boolean> arrayList = new ArrayList<>();
        int length = str.length;
        for (int i = 0; i < length; i++) {
            if(i==0){
                arrayList.add(i,true);
            }else{
            arrayList.add(i, false);
            }
        }
        return arrayList;
    }

    public static ArrayList<Boolean> getCropArryList(CropsBean cropsBean,int selectId) {
        ArrayList<Boolean> arrayList = new ArrayList<>();
        int length=cropsBean.getV().getALLCROPS().size();
        for (int i = 0; i < length; i++) {
            if(i==selectId){
                arrayList.add(i,true);
            }else{
                arrayList.add(i, false);
            }
        }
        return arrayList;
    }

    //判断是否有选中的日期
    public static ArrayList<Boolean> getSignList(int length, ArrayList<String> signDay) {
        ArrayList<Boolean> arrayList = new ArrayList<>();
        arrayList.clear();
        int size = signDay.size();
        for (int i = 0; i < length; i++) {
            arrayList.add(false);
            for (int j = 0; j < size; j++) {
                int mb = i + 1;
                if (signDay.get(j).equals(mb + "")) {
                    arrayList.set(i, true);
                }
            }
        }
        return arrayList;
    }

    public static ArrayList<String> getArrayList(String[] str, String content) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.clear();
        if (str != null) {
            int length = str.length;
            for (int i = 0; i < length; i++) {
                arrayList.add(str[i]);
            }
        } else {
            arrayList.add(content);
        }
        return arrayList;
    }

    //选择的内容 gap为间隔符
    public static StringBuffer getStringBuffer(ArrayList<Boolean> list, String[] data, String gap) {
        StringBuffer stringBuffer = new StringBuffer();
        int length = list.size();
        for (int i = 0; i < length; i++) {
            if (list.get(i)) {
                stringBuffer.append(data[i] + gap);
            }
        }
        return stringBuffer;
    }
    //将字符串中的/改为，

    /**
     * @param str  传入的字符串
     * @param olds 需要改变的字符
     * @param news 新的字符
     * @return
     */
    public static String sprithangecomma(String str, String olds, String news) {
        return str.replaceAll(olds, news);
    }

    //截取最后一个斜杠后面的字符串
    public static String sprintlaststring(String str,String tyep) {
        String[] strs = str.split(tyep);
        return strs[strs.length - 1];
    }

    //截取最后一个斜杠之前的字符串
    public static String sprintbeforstring(String str, String type) {
        if (str.endsWith(type)) {
            int length = str.length();
            return str.substring(0, length - 1);
        }
        return str;
    }

    //返回字符串数组
    public static String[] address(String str, String type) {
        if (str.contains(type)) {
            return str.split(type);
        }
        return null;
    }

    public static Integer getaddress(String str) {
        if (str != null) {
            return 1;
        }
        return 0;
    }

    //判断Array数组里面是否有true值
    public static boolean isSelect(ArrayList<Boolean> list) {
        int length = list.size();
        for (int i = 0; i < length; i++) {
            if (list.get(i)) {
                return true;
            }
        }
        return false;
    }

    //判断有没有小数点
    public static boolean isXiaoShu(String number) {
        return number.contains(".");
    }

    //判断小数点的位数
    public static int length(String number) {
        String m = number.substring(number.indexOf(".") + 1);
        return m.trim().length();
    }

    // 解析时间
    public static String paserTime(int time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String times = format.format(new Date(time * 1000L));
        return times;
    }
    // 解析时间
    public static String paserTTime(int time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String times = format.format(new Date(time * 1000L));
        return times;
    }

    public static String paserDate(int time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String times = format.format(new Date(time * 1000L));
        return times;
    }

    /**
     * 添加，
     */
    public static String getAdd(ArrayList<String> arrayList) {
        int legnth = arrayList.size();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < legnth; i++) {
            sb.append(arrayList.get(i));
            sb.append(",");
        }
        return sb.toString();
    }

    /**
     * 去掉最后一个符号
     */
    public static String getAddres(ArrayList<String> arrayList) {
        int legnth = arrayList.size();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < legnth; i++) {
            sb.append(arrayList.get(i));
            sb.append(",");
        }
        return sb.toString().substring(0, sb.toString().length() - 1);
    }

    /**
     * 去掉第一个0
     */
    public static Integer getTime(String time) {
        String times = sprithangecomma(time, ":", "");
        if (times.startsWith("0")) {
            return Integer.valueOf(times.replaceFirst("0", ""));
        }
        return Integer.valueOf(times);
    }

    /**
     * 平均天数
     */
    public static String[] getDay(ArrayList<String> arrayList, String number) {
        int length = arrayList.size();
        int avg = Integer.valueOf(number) / length;
        int yushu = Integer.valueOf(number) % length;
        String[] day = new String[length];
        for (int i = 0; i < length; i++) {
            if ((i + 1) == length) {
                day[i] = avg + yushu + "";
            } else {
                day[i] = avg + "";
            }
        }
        return day;
    }

    //平均地方
    public static String[] getAddress(ArrayList<String> arrayList, String number) {
        int length = arrayList.size(); //城市
        int size = Integer.valueOf(number); //天数
        //如果城市数大于或者等于天数执行这个
        if (length >= size) {
            int avg = length / size;
            int yushu = length % size;
            String[] address = new String[size];
            for (int i = 0; i < size; i++) {
                if ((i + 1)== size) {
                    StringBuffer sb = new StringBuffer();
                    for (int j = 0; j <yushu+avg; j++) {
                        sb.append(arrayList.get(i * avg + j));
                        sb.append(",");
                    }
//                }
                    address[i] = sb.toString();
                } else {
                    //0
                    StringBuffer sb = new StringBuffer();
                    for (int j = 0; j <avg; j++) {
                            sb.append(arrayList.get(avg * i + j));
                            sb.append(",");
                    }
                    address[i] = sb.toString();

                }
            }
            return address;
        } else {
            //天数大于城市数了
            String[] address = new String[size];
            for (int i = 0; i < size; i++){
                if ((i+1)<=length){
                    address[i] = arrayList.get(i)+",";
                } else {
                    address[i] = "";
                }
            }
            return  address;
        }
    }

    //计算所有的天数
    public static int getNumber(String[] day) {
        int legnth = day.length;
        int number = 0;
        for (int i = 0; i < legnth; i++) {
            number = number + Integer.valueOf(day[i]);
        }
        return number;
    }

    //用于判断等级
    public static int getLev(int exp) {
        if (exp>=100000) {
            return 5;
        }
        if (exp>=10000) {
            return 4;
        }
        if (exp>=1000) {
            return 3;
        }
        if (exp>=100) {
            return 2;
        }
        if (exp>=10) {
            return 1;
        }
        return 0;
    }

    public static String getPingJia(int bar){
        if(bar>=4){
            return "好";
        }
        if(bar>=2){
            return "中";
        }
        return "差";
    }

    //字符非空判断
  public static boolean isNull(String str){
      if(str != null&&!str.equals("null")&& str.length() != 0) {
          return true;
      }
      return false;
  }
}
